package com.xof.phonecall

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import com.xof.phonecall.base.BaseActivity
import com.xof.phonecall.databinding.ActivityVersionUpdateBinding
import com.xof.phonecall.helpipetrpide.versionUtil.ZHIVersionClient
import com.xof.phonecall.helpipetrpide.versionUtil.ZHIVersionIMPL
import com.xof.phonecall.valipetrpide.ValPoint

class VersionUpdateActivity : BaseActivity() {

    lateinit var mActivityBinding: ActivityVersionUpdateBinding

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        return list
    }

    override fun onBindingViewTag(): String {
        return ValPoint.LOGIN.name
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivityVersionUpdateBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onContentViewBinded() {
        try {
            //登录返回
            getNeedPermissions()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onPermissionsRequestAllPassipetrpide() {
        ZHIVersionClient.startDownload(ZHIVersionIMPL.productLink) { currentBytes, contentLength, done ->
            val progress = (currentBytes * 100 / contentLength).toInt()
            mActivityBinding.pbProgress.progress = progress
            mActivityBinding.tvProgress.text = "$progress%"
            mActivityBinding.tvProgress.visibility = View.VISIBLE
            if (done) {
                //安装文件
                ZHIVersionIMPL.startInstallApk(ZHIVersionIMPL.productApkLocalPath)
            }
        }
    }
}